#include "TCPServerPacket.h"

namespace network
{

network::TCPServerDispatcherPacket::TCPServerDispatcherPacket(TCPServerPacket::Ptr& packet)
{
	if (packet->isZiped())
	{
		//throw new Exception();
	}
	else
	{
		_nDataLen = packet->getDataSize();
		packet->moveData(_pDataBuf);
	}
}

TCPServerDispatcherPacket::~TCPServerDispatcherPacket()
{
	if (_pDataBuf != nullptr)
		delete[] _pDataBuf;
}

TCPServerDispatcherPacket::TCPServerDispatcherPacket(TCPServerDispatcherPacket& packet)
{
	_nDataLen = packet._nDataLen;
	_pDataBuf = new char[_nDataLen];
	memcpy((void *)_pDataBuf, (void *)packet._pDataBuf, _nDataLen);
}

TCPServerDispatcherPacket::TCPServerDispatcherPacket(TCPServerDispatcherPacket* packet)
{
	_nDataLen = packet->_nDataLen;
	_pDataBuf = new char[_nDataLen];
	memcpy((void *)_pDataBuf, (void *)packet->_pDataBuf, _nDataLen);
}

TCPServerDispatcherPacket::TCPServerDispatcherPacket(TCPServerDispatcherPacket&& packet)
{
	_nDataLen = packet._nDataLen;
	_pDataBuf = packet._pDataBuf;
	packet._pDataBuf = nullptr;
}

TCPServerDispatcherPacket& TCPServerDispatcherPacket::operator =(TCPServerDispatcherPacket& packet)
{
	_nDataLen = packet._nDataLen;
	_pDataBuf = new char[_nDataLen];
	memcpy((void *)_pDataBuf, (void *)packet._pDataBuf, _nDataLen);

	return *this;
}

TCPServerDispatcherPacket& TCPServerDispatcherPacket::operator =(TCPServerDispatcherPacket* packet)
{
	_nDataLen = packet->_nDataLen;
	_pDataBuf = new char[_nDataLen];
	memcpy((void *)_pDataBuf, (void *)packet->_pDataBuf, _nDataLen);

	return *this;
}

TCPServerDispatcherPacket& TCPServerDispatcherPacket::operator =(TCPServerDispatcherPacket&& packet)
{
	_nDataLen = packet._nDataLen;
	_pDataBuf = packet._pDataBuf;
	packet._pDataBuf = nullptr;

	return *this;
}

TCPServerPacket::TCPServerPacket()
: _header() {}

TCPServerPacket::~TCPServerPacket()
{
	if (_pszData != nullptr)
	{
		delete [] _pszData;
		_pszData = nullptr;
	}
}

TCPServerPacket::TCPServerPacket(TCPServerPacket& packet)
{
	_header = packet._header;
	allocData();
	memcpy(_pszData, packet._pszData, (UInt32)packet.getDataSize());
}

TCPServerPacket::TCPServerPacket(TCPServerPacket&& packet)
{
	_header = packet._header;
	_pszData = packet._pszData;
	packet._pszData = nullptr;
}

TCPServerPacket& TCPServerPacket::operator =(TCPServerPacket& packet)
{
	_header = packet._header;
	allocData();
	memcpy(_pszData, packet._pszData, (UInt32)packet.getDataSize());

	return *this;
}

Int32 TCPServerPacket::getChannelID()
{
	return _header.nChannelID;
}

bool TCPServerPacket::isZiped()
{
	return _header.blZipFlag == 1;
}
void TCPServerPacket::moveData(char*& pBuf)
{
	pBuf = _pszData;
	_pszData = nullptr;
}

char* TCPServerPacket::getHeader()
{
	return (char *) &_header;
}

Int32 TCPServerPacket::getHeaderSize()
{
	return sizeof(_header);
}

bool TCPServerPacket::validateHeader(std::string& strErrMsg)
{
	return true;
}

void TCPServerPacket::allocData()
{
	_pszData = new char[_header.nDataLen];
}

char *TCPServerPacket::getData()
{
	return _pszData;
}

Int32 TCPServerPacket::getDataSize()
{
	return _header.nDataLen;
}

bool TCPServerPacket::validateData(std::string& strErrMsg)
{
	return true;
}

}

